<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/4/7
 * Time: 15:34
 */
namespace Api\Controller;

use Think\Controller;


/**
 *极光推送操作
 */
class JpushController extends BaseController
{
    public function index(){
        //
      /*  $this->pushMessage();
        echo 'ok';
        exit;*/
        while(true){
            try {
                $this->pushMessage();
            } catch (\Exception $e) {
                if (strpos($e->getMessage(), 'Unknown response') !== false) {
                    echo date('y-m-d h:i:s') . "rss_pop redis bug continue" . PHP_EOL;
                    continue;
                } else {
                    echo $e->getMessage();
                    echo $e->getFile() . " Line:";
                    echo $e->getLine() . "<br>";
                    echo date('y-m-d h:i:s') . " rss_pop other bug continue" . PHP_EOL;
                    continue;
                }
            }
            sleep(rand() % 3);
        }

    }


    public function text(){
        //jpushStr('13729747247','抢单成功！订单号为：12121212','tb');
       // $juli=getDistance(I('post.lat1'),I('post.lng1'),I('post.lat2'),I('post.lng2'));
        //echo $juli;exit;

        $data=array('page'=>'ceshishsihsihsi','id'=>2);
        //$data=json_encode($data);

        $jpushObj = new \Org\Util\JPush_test();
        $receive = 'all';      //别名
        $content = '谁收到了，应我一声';   //内容
        $m_type = 'tp';
        $pushData=$data;
        $m_txt = $data;
        $m_time = 600;        //离线保留时间
        $res = $jpushObj->send_pub($receive, $content, $m_type, $m_txt, $m_time);
        var_dump($res);
    }

    
    private function pushMessage(){

        $redis = S(array('type' => 'redis'));
        //得到推送信息
        $jpush = $redis->lpop(C('JPUSH_KEY'));
       if($jpush){
            $jpush=unserialize($jpush);
            $jpushObj = new \Org\Util\JPush_test();
            $receive = $jpush['alias'];      //别名
            $content = $jpush['content'];   //内容
            $m_type = 'tb';
            $pushData=$jpush['data'];
            $m_txt = json_encode($pushData);
            $m_time = $jpush['m_time'];        //离线保留时间
            $res = $jpushObj->send_pub($receive, $content, $m_type, $m_txt, $m_time);
        }

    }
}